// 导入router包
const Router = require('koa-router')
// 实例化对象
const CartRouter = new Router({ prefix: '/address' })
// 权限中间件
const { auth } = require('../middleware/auth.middleware')
const { paramValidator } = require('../middleware/address.middleware')

// 添加购物车
const { create, findAll, update, remove, setDefault } = require('../controllers/address.controllers')

CartRouter.post('/', auth, paramValidator({
    consignee: "string",
    phone: {
        type: "string",
        format: /^1\d{10}$/,
    },
    address: {
        type: "string",
    }
}), create)

CartRouter.get('/', auth, findAll)
// 修改
CartRouter.put('/:id', auth, paramValidator({
    consignee: "string",
    phone: {
        type: "string",
        format: /^1\d{10}$/,
    },
    address: {
        type: "string",
    }
}), update)
// 删除
CartRouter.delete('/:id', auth, remove)
// 默认地址 加入无效ID 不存在
CartRouter.patch('/:id', auth, setDefault)
module.exports = CartRouter